<?php

include("global.php");

$pagetitle=" - Administration - Forums";
$links=" > Administration > Forums";

include ("header.php");

include ("admin_header.php");

if($userloggedinarray[15]!=="administrator"){
	unset($tplcontent);
	$tplcontent['notice']="Must be logged in as administrator to use control panel!";
	template('notice',$tplcontent);
}else{
	
	if(!isset($editforum)&&!isset($editforum2)&&!$addforum&&!$addforum2&&!isset($deleteforum)&&!isset($editorder)){
		unset($tplcontent);
		
		function forumsadminlist($parentforum,$level){
			global $configarray,$forumlist;
			
			if($forumlist[$parentforum]){
				$forumsublist=explode(",",$forumlist[$parentforum]);
			}
			
			for($n=0;$n<count($forumsublist);$n++){
				unset($tplcontent2);
				$forumconfigarray=getdata("$configarray[2]/$forumsublist[$n].php");
				
				for($m=0;$m<$level;$m++){
					$tplcontent2['forumname'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				
				$tplcontent2['forumname'].="<b>$forumconfigarray[3]</b> ($forumconfigarray[5])";
				
				$tplcontent2['id'].="ID:$forumsublist[$n]";
				
				$tplcontent2['edit'].="<a href=\"admin_forums.php?editforum=$forumsublist[$n]\">Edit</a>";
				
				$tplcontent2['delete'].="<a href=\"admin_forums.php?deleteforum=$forumsublist[$n]\">Delete</a>";
				
				$tplcontent['forumrows'].=template('adminforumrow',$tplcontent2);
				
				$tplcontent['forumrows'].=forumsadminlist($forumsublist[$n],$level+1);
				
			}//loop
			
			return $tplcontent['forumrows'];
		}//function
		
		$tplcontent['forumrows'].=forumsadminlist("0",0);
		
		echo template('adminforumsview',$tplcontent);
		
	}
	
	if(isset($editforum)){
		unset($tplcontent);
		$forumconfigarray=getdata("$configarray[2]/$editforum.php");
		$tplcontent['formheader'].="<form action=\"admin_forums.php\" method=post>";
		$tplcontent['formheader'].="<input type=hidden name=\"editforum2\" value=\"$editforum\">";
		
		$tplcontent['id'].="ID: $editforum";
		
		$tplcontent['forumname'].="<input type=text name=\"name\" value=\"$forumconfigarray[3]\" size=40 class=\"forminput\">";
		
		$tplcontent['description'].="<input type=text name=\"description\" value=\"$forumconfigarray[2]\" size=80 class=\"forminput\">";
		
		$tplcontent['parentforum'].="<select size=1 name=\"parentforum\" size=40 class=\"forminput\">";
		
		$tplcontent['parentforum'].="<option value=\"0\">No Parent</option>";
		$tplcontent['parentforum'].=forumsmenu(0,0,$forumconfigarray[4]);
		$tplcontent['parentforum'].="</select>";
		
		
		$tplcontent['function'].="<select size=1 name=\"function\" size=40 class=\"forminput\">\n";
		if($forumconfigarray[5]=="category"){
			$tplcontent['function'].="<option value=\"forum\">Forum</option>";
			$tplcontent['function'].="<option value=\"category\" selected>Category</option>";
		}else{
			$tplcontent['function'].="<option value=\"forum\" selected>Forum</option>";
			$tplcontent['function'].="<option value=\"category\">Category</option>";
		}
		$tplcontent['function'].="</select>";
		
		//VIEW ACCESS
		$tplcontent['view'].="Which <a href=\"admin_usergroups.php\">user groups</a>  don't have access to view this forum (separated by commas)<br>";
		$tplcontent['view'].="<input type=text name=\"viewaccess\" value=\"$forumconfigarray[0]\" size=60 class=\"forminput\">";
		
		//NEW THREAD ACCESS
		$tplcontent['newtopic'].="Which <a href=\"admin_usergroups.php\">user groups</a> don't have access to start new topics in this forum (separated by commas)<br>";
		$tplcontent['newtopic'].="<input type=text name=\"newthreadaccess\" value=\"$forumconfigarray[7]\" size=60 class=\"forminput\">";
		
		//REPLY ACCESS
		$tplcontent['reply'].="Which <a href=\"admin_usergroups.php\">user groups</a>  don't have access to reply in this forum (separated by commas)<br>";
		$tplcontent['reply'].="<input type=text name=\"replyaccess\" value=\"$forumconfigarray[8]\" size=60 class=\"forminput\">";
		
		//EDIT ACCESS
		$tplcontent['edit'].="Which <a href=\"admin_usergroups.php\">user groups</a> don't have access to edit their posts in this forum (separated by commas)<br>";
		$tplcontent['edit'].="<input type=text name=\"editaccess\" value=\"$forumconfigarray[9]\" size=60 class=\"forminput\">";
		
		//SHOW ACCESS
		$tplcontent['show'].="Which <a href=\"admin_usergroups.php\">user groups</a> don't have access to see this forum (Ex: It will be hidden to them) (separated by commas)<br>";
		$tplcontent['show'].="<input type=text name=\"showaccess\" value=\"$forumconfigarray[10]\" size=60 class=\"forminput\">";
		
		$tplcontent['moderators'].="(listed as <b>userid</b> NOT username, separate each id with comma)<br>";
		$tplcontent['moderators'].="<input type=text name=\"moderators\" value=\"$forumconfigarray[1]\" size=50 class=\"forminput\">";
		
		
		if($forumconfigarray[6]=="on"){
			$tplcontent['postincrease'].="<input type=checkbox name=\"postincrease\" class=\"forminput\" checked>";
		}else{
			$tplcontent['postincrease'].="<input type=checkbox name=\"postincrease\" class=\"forminput\">";
		}
		
		$tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Update\" class=\"formbutton\">";
		
		$tplcontent['formfooter'].="</form>";
		
		echo template('adminforumedit',$tplcontent);
	}
	
	if(isset($editforum2)){
		$forumconfigarray=getdata("$configarray[2]/$editforum2.php");
		
		if($parentforum!==$forumconfigarray[4]){
			if($forumlist[$forumconfigarray[4]]){$forumsublist=@explode(",",$forumlist[$forumconfigarray[4]]);}
			for($n=0;$n<count($forumsublist);$n++){
				if($forumsublist[$n]==$editforum2){
					unset($forumsublist[$n]);
				}
			}
			$forumsubline=@implode(",",$forumsublist);
			writedata("$configarray[2]/list.php",$forumsubline,$forumconfigarray[4]);
			
			if($forumlist[$parentforum]){$forumsublist=@explode(",",$forumlist[$parentforum]);}
			@array_push($forumsublist,$editforum2);
			$forumsubline=@implode(",",$forumsublist);
			writedata("$configarray[2]/list.php",$forumsubline,$parentforum);
		}
		
		writedata("$configarray[2]/$editforum2.php",$viewaccess,0,array('forum'=>$editforum2));
		writedata("$configarray[2]/$editforum2.php",$moderators,1,array('forum'=>$editforum2));
		$varname=$forumarray[$n] . "description";
		$description=stripslashes($description);
		$description=strip_tags($description);
		$description=htmlentities($description);
		writedata("$configarray[2]/$editforum2.php",$description,2,array('forum'=>$editforum2));
		$name=stripslashes($name);
		$name=strip_tags($name);
		$name=htmlentities($name);
		writedata("$configarray[2]/$editforum2.php",$name,3);
		writedata("$configarray[2]/$editforum2.php",$parentforum,4);
		writedata("$configarray[2]/$editforum2.php",$function,5);
		writedata("$configarray[2]/$editforum2.php",$postincrease,6);
		writedata("$configarray[2]/$editforum2.php",$newthreadaccess,7);
		writedata("$configarray[2]/$editforum2.php",$replyaccess,8);
		writedata("$configarray[2]/$editforum2.php",$editaccess,9);
		writedata("$configarray[2]/$editforum2.php",$showaccess,10);
		
		
		
		unset($tplcontent);
		$tplcontent['notice'].="Forum information updated";
		echo template('notice',$tplcontent);
	}
	
	if($addforum){
		unset($tplcontent);
		$tplcontent['formheader'].="<form action=\"admin_forums.php\" method=post>";
		$tplcontent['formheader'].="<input type=hidden name=\"addforum2\" value=\"1\" size=40>";
		
		$tplcontent['forumname'].="<input type=text name=\"name\" size=40 class=\"forminput\">";
		
		$tplcontent['description'].="<input type=text name=\"description\" size=80 class=\"forminput\">";
		
		$tplcontent['parentforum'].="<select size=1 name=\"parentforum\" size=40 class=\"forminput\">";
		$tplcontent['parentforum'].="<option value=\"0\">No Parent</option>";
		$tplcontent['parentforum'].=forumsmenu(0,0);
		$tplcontent['parentforum'].="</select>";
		
		$tplcontent['function'].="<select size=1 name=\"function\" size=40 class=\"forminput\">\n";
		$tplcontent['function'].="<option value=\"forum\" selected>Forum</option>";
		$tplcontent['function'].="<option value=\"category\">Category</option>";
		
		//VIEW ACCESS
		$tplcontent['view'].="Which <a href=\"admin_usergroups.php\">user groups</a>  don't have access to view this forum (separated by commas)<br>";
		$tplcontent['view'].="<input type=text name=\"viewaccess\" size=60 class=\"forminput\">";
		
		//NEW THREAD ACCESS
		$tplcontent['newtopic'].="Which <a href=\"admin_usergroups.php\">user groups</a> don't have access to start new topics in this forum (separated by commas)<br>";
		$tplcontent['newtopic'].="<input type=text name=\"newthreadaccess\" value=\"guest\" size=60 class=\"forminput\">";
		
		//REPLY ACCESS
		$tplcontent['reply'].="Which <a href=\"admin_usergroups.php\">user groups</a>  don't have access to reply in this forum (separated by commas)<br>";
		$tplcontent['reply'].="<input type=text name=\"replyaccess\" value=\"guest\" size=60 class=\"forminput\">";
		
		//EDIT ACCESS
		$tplcontent['edit'].="Which <a href=\"admin_usergroups.php\">user groups</a> don't have access to edit their posts in this forum (separated by commas)<br>";
		$tplcontent['edit'].="<input type=text name=\"editaccess\" size=60 value=\"guest\" class=\"forminput\">";
		
		//SHOW ACCESS
		$tplcontent['show'].="Which <a href=\"admin_usergroups.php\">user groups</a> don't have access to see this forum (Ex: It will be hidden to them) (separated by commas)<br>";
		$tplcontent['show'].="<input type=text name=\"showaccess\" size=60 class=\"forminput\">";
		
		$tplcontent['moderators'].="(listed as <b>userid</b> NOT username, separate each id with comma)<br>";
		$tplcontent['moderators'].="<input type=text name=\"moderators\" size=50 class=\"forminput\">";
		
		$tplcontent['postincrease'].="<input type=checkbox name=\"postincrease\" class=\"forminput\" checked>";
		
		$tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Add forum\" class=\"formbutton\">";
		
		
		$tplcontent['formfooter'].="</form>";
		
		echo template('adminforumadd',$tplcontent);
	}
	
	if(isset($addforum2)){
		
		@rsort($forumarray,SORT_NUMERIC);
		
		$newforumid=$forumarray[0]+1;
		
		createdir("$configarray[2]/$newforumid");
		
		$description=htmlentities(strip_tags(stripslashes($description)));
		$name=htmlentities(strip_tags(stripslashes($name)));
		
		$dataarray=array(
		0=>$viewaccess,
		1=>$moderators,
		2=>$description,
		3=>$name,
		4=>$parentforum,
		5=>$function,
		6=>$postincrease,
		7=>$newthreadaccess,
		8=>$replyaccess,
		9=>$editaccess,
		10=>$showaccess,
		11=>"0");
		writearray("$configarray[2]/$newforumid.php",$dataarray,0);
		
		if($forumlist[$parentforum]){
			$forumsublist=@explode(",",$forumlist[$parentforum]);
			@array_push($forumsublist,$newforumid);
			$forumsubline=@implode(",",$forumsublist);
		}else{
			$forumsubline=$newforumid;
		}
		writedata("$configarray[2]/list.php",$forumsubline,$parentforum);
		
		unset($tplcontent);
		$tplcontent['notice']="New forum created!";
		echo template('notice',$tplcontent);
		
	}
	
	if(isset($deleteforum)){
		
		$forumconfigarray=allforumconfigarray($deleteforum);
		if($forumlist[$forumconfigarray[4]]){$forumsublist=@explode(",",$forumlist[$forumconfigarray[4]]);}
		for($n=0;$n<count($forumsublist);$n++){
			if($forumsublist[$n]==$deleteforum){
				unset($forumsublist[$n]);
			}
		}
		$forumsubline=@implode(",",$forumsublist);
		writedata("$configarray[2]/list.php",$forumsubline,$forumconfigarray[4]);
		
		$threadarray=listdirs("$configarray[2]/$deleteforum");
		for($n=0;$n<count($threadarray);$n++){
			$postarray=listfiles("$configarray[2]/$deleteforum/$threadarray[$n]");
			for($m=0;$m<count($postarray);$m++){
				@unlink("$configarray[2]/$deleteforum/$threadarray[$n]/$postarray[$m].php");
			}
			@rmdir("$configarray[2]/$deleteforum/$threadarray[$n]");
		}
		$threadcfgarray=listfiles("$configarray[2]/$deleteforum");
		for($n=0;$n<count($threadcfgarray);$n++){
			@unlink("$configarray[2]/$deleteforum/$threadcfgarray[$n].php");
		}
		@unlink("$configarray[2]/$deleteforum.php");
		@rmdir("$configarray[2]/$deleteforum");
		
		unset($tplcontent);
		$tplcontent['notice']="Forum '$deleteforum' deleted";
		
		echo template('notice',$tplcontent);
		
	}//remove bracket
	
	if($editorder=="1"){
		unset($tplcontent);
		
		$tplcontent['formheader'].="<form action=\"admin_forums.php\" method=post>";
		$tplcontent['formheader'].="<input type=hidden name=\"editorder\" value=\"2\" size=40>";
		
		unset($tplcontent2);
		
		for($m=0;$m<$level;$m++){
			$tplcontent2['forumname'].="&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		
		$tplcontent2['forumname'].="<b>Parent forum (top level)</b>";
		
		$tplcontent2['id'].="0";
		
		$tplcontent2['currentorderlist'].=$forumlist[0]."&nbsp;";
		
		$tplcontent2['neworderlist'].="<input type=text name=\"order0\" value=\"".$forumlist[0]."\" size=20 class=\"forminput\">";
		
		$tplcontent['forumorderrows'].=template('adminforumorderrow',$tplcontent2);
		
		function forumsorderlist($parentforum,$level){
			global $configarray,$forumlist;
			
			if($forumlist[$parentforum]){$forumsublist=explode(",",$forumlist[$parentforum]);}
			for($n=0;$n<count($forumsublist);$n++){
				unset($tplcontent2);
				$forumconfigarray=getdata("$configarray[2]/$forumsublist[$n].php");
				
				for($m=0;$m<$level;$m++){
					$tplcontent2['forumname'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				
				$tplcontent2['forumname'].="<b>$forumconfigarray[3]</b>";
				
				$tplcontent2['id'].="$forumsublist[$n]";
				
				$tplcontent2['currentorderlist'].=$forumlist[$forumsublist[$n]]."&nbsp;";
				
				$tplcontent2['neworderlist'].="<input type=text name=\"order$forumsublist[$n]\" value=\"".$forumlist[$forumsublist[$n]]."\" size=20 class=\"forminput\">";
				
				$tplcontent['forumorderrows'].=template('adminforumorderrow',$tplcontent2);
				
				$tplcontent['forumorderrows'].=forumsorderlist($forumsublist[$n],$level+1);
			}//loop
			
			return $tplcontent['forumorderrows'];
		}//function
		
		$tplcontent['forumorderrows'].=forumsorderlist("0",0);
		
		$tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Update\" class=\"formbutton\">";
		
		$tplcontent['formfooter'].="</form>";
		
		echo template('adminforumsorder',$tplcontent);
		
	}//order bracket
	
	if($editorder=="2"){
		
		$orderarray=explode(",",$order0);
		for($n=0;$n<count($orderarray);$n++){
			writedata("$configarray[2]/$orderarray[$n].php","0",4);
		}
		
		writedata("$configarray[2]/list.php",$order0,0);
		
		function forumsorderlist2($parentforum,$level){
			global $configarray,$forumlist;
			
			if($forumlist[$parentforum]){$forumsublist=explode(",",$forumlist[$parentforum]);}
			for($n=0;$n<count($forumsublist);$n++){
				writedata("$configarray[2]/$forumsublist[$n].php",$parentforum,4);
				$forumconfigarray=getdata("$configarray[2]/$forumsublist[$n].php");
				
				$val="order".$forumsublist[$n];
				global ${$val};
				writedata("$configarray[2]/list.php",${$val},$forumsublist[$n]);
				
				forumsorderlist2($forumsublist[$n],$level+1);
			}//loop
		}//function
		
		forumsorderlist2("0",0);
		
		unset($tplcontent);
		$tplcontent['notice'].="<span class=\"textlarge\">Forum ordering updated</span>";
		echo template('notice',$tplcontent);
		
	}//order2 bracket
	
}//admin check bracket

include("admin_footer.php");

include("footer.php");

?>
